﻿Imports System.Net.NetworkInformation
Imports MySql.Data.MySqlClient
Imports System.IO

Public Class MainWindow

    ''Adds the Network Handler
    Public RegisteredProduct As Boolean = False

    Public Sub New()

        ' This call is required by the designer. IE don't touch it genuis
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddHandler NetworkChange.NetworkAvailabilityChanged, AddressOf InternetChecks.NetworkStateChange
    End Sub


    Private Sub MainWindowLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If InternetChecks.CheckInternetSvr() Then
            MySQLManager.Connect()
            MySQLManager.LoadData("", "")
        End If
        AdManager.GetAd(My.Resources.WSI_Ad_URL)
    End Sub

    ' Menu Links
    Private Sub CreditsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreditsToolStripMenuItem.Click
        Credits.ShowDialog()
    End Sub

    Private Sub PreferencesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreferencesToolStripMenuItem.Click '
        PreferencesDialog.ShowDialog()
    End Sub

    Private Sub SourcesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  '
        SourcesDialog.ShowDialog()
    End Sub

    Private Sub UpdateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateToolStripMenuItem.Click '
        UpdateManager.ShowDialog()
    End Sub

    Private Sub GetHelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetHelpToolStripMenuItem.Click '
        Process.Start(My.Resources.WSI_Help_URL)
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click '
        Me.Close()
        Me.Dispose()
    End Sub

    Private Sub AboutPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutPageToolStripMenuItem.Click '
        AboutPage.ShowDialog()
    End Sub

    Private Sub ExampleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ConfigTest.ShowDialog()
    End Sub

    Private Sub AutoupdToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        AutoUpdate_Download.Show()
    End Sub

    Private Sub UpgradeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpgradeToolStripMenuItem.Click
        Process.Start(My.Resources.WSI_Upgrade_URL)
    End Sub

    Private Sub OpenDownloadsFolderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenDownloadsFolderToolStripMenuItem.Click
        Dim file As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\User.xml"
        Dim downloadFolder As String = ReadXML.Read(file, "UserConfig", "Downloads")
        If downloadFolder = "" Then
            downloadFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\WSI Downloads\"
        End If
        If Not Directory.Exists(downloadFolder) Then
            Try
                Directory.CreateDirectory(downloadFolder)
                Process.Start("explorer.exe", downloadFolder)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        Else
            Try
                Process.Start(downloadFolder)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Dim Download_URL As String = ""
    Dim cleanFolderName As String = ""
    Dim ExeFile As String = ""
    Public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim val As Integer = DataGridView1.CurrentCellAddress.Y
        If val < 0 Then
            val = 0
        End If
        Try
            DescriptionTextbox.Text = DataGridView1.Item(4, val).Value
            Download_URL = DataGridView1.Item(5, val).Value
            cleanFolderName = DataGridView1.Item(6, val).Value
            ExeFile = DataGridView1.Item(7, val).Value
            Dim row As Integer
            row = DataGridView1.CurrentCellAddress.Y
            DataGridView1.Rows(row).Selected = True
        Catch ex As Exception

        End Try


    End Sub

    Private Sub Btn_download_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_download.Click
        Dim path As String = ""
        If ReadXML.Read(Environment.SpecialFolder.LocalApplicationData & "\WSI\Config\User.xml", "UserConfig", "Downloads") = "" Then
            path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\WSI Downloads"
        Else
            path = ReadXML.Read(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\User.xml", "UserConfig", "Downloads")
        End If
        Try
            DownloadClass.GetFile(Download_URL, path & "\" & cleanFolderName)
        Catch ex As Exception
            MsgBox("Failed to Download File")
        End Try
        Try
            Shell(path & cleanFolderName & "\" & ExeFile)
            MsgBox(path & cleanFolderName & "\" & ExeFile)
        Catch ex As Exception
            Process.Start("explorer.exe", path & cleanFolderName)
            MsgBox("second")
        End Try

    End Sub
    Dim category As String = ""
    Dim i As Integer = 0
    Private Sub SearchRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchRefresh.Tick
        If i = 1 Then
            MySQLManager.LoadData(Search_textbox.Text, category)
            i = 0
        End If
    End Sub

    Private Sub Search_Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search_Go.Click
        SearchRefresh.Enabled = 1
        i = 1
    End Sub

    Private Sub cat_All_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cat_All.Click
        category = ""
        SearchRefresh.Enabled = 1
        i = 1
    End Sub

    Private Sub cat_Office_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cat_Office.Click
        category = "Office"
        SearchRefresh.Enabled = 1
        i = 1
    End Sub

    Private Sub Cat_Games_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cat_Games.Click
        category = "Games"
        SearchRefresh.Enabled = 1
        i = 1
    End Sub

    Private Sub cat_Security_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cat_Security.Click
        category = "Security"
        SearchRefresh.Enabled = 1
        i = 1
    End Sub

    Private Sub cat_Utilities_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cat_Utilities.Click
        category = "Utilities"
        SearchRefresh.Enabled = 1
        i = 1
    End Sub

    Private Sub cat_Graphics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cat_Graphics.Click
        category = "Graphics"
        SearchRefresh.Enabled = 1
        i = 1
    End Sub

    Private Sub cat_Sound_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cat_Sound.Click
        category = "Sound"
        SearchRefresh.Enabled = 1
        i = 1
    End Sub




End Class
